package com.example.swordjava;


import androidx.annotation.Nullable;

import java.util.Stack;

public class Test33 {

    public static void main(String[] args) {

        int[][] a = {{1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}};

        System.out.println(a[0][1]);

    }

    class TreeNode {
        TreeNode left = null;
        TreeNode right = null;
        int val;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    /**
     * 二叉搜索树的后序遍历序列
     * 输入一个整数数组，判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回 true ,否则返回 false 。假设输入的数组的任意两个数字都互不相同。
     */

    public boolean VerifySquenceOfBST(int[] sequence) {
        if (sequence.length == 0) return false;
        int start = 0;
        int root = sequence.length - 1;
        return helper(start,root,sequence);
    }

    public boolean helper(int start, int root, int[] a) {
        if (start >= root) return true;
        int i = root;
        while (start < i && a[i-1] > a[root]) i--;
        for (int j = i - 1; j >= start; j--) {
            if (a[j] >= a[root]) return false;
        }
        return helper(start,i-1,a)&&helper(i,root-1,a);
    }
}
